/*
Copyright 2023 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package loadbalancer

import (
	"testing"

	. "github.com/onsi/gomega"
)

func TestConfig(t *testing.T) {
	g := NewWithT(t)

	testCases := []struct {
		name           string
		data           *ConfigData
		configTemplate string
		expectedConfig string
	}{
		{
			name: "should return default HA proxy config",
			data: &ConfigData{
				BackendControlPlanePort:  "6443",
				FrontendControlPlanePort: "7777",
				BackendServers: map[string]BackendServer{
					"control-plane-0": {
						Address: "1.1.1.1",
						Weight:  99,
					},
				},
			},
			configTemplate: DefaultTemplate,
			expectedConfig: `# generated by kind
global
  log /dev/log local0
  log /dev/log local1 notice
  daemon
  # limit memory usage to approximately 18 MB
  # (see https://github.com/kubernetes-sigs/kind/pull/3115)
  maxconn 100000

resolvers docker
  nameserver dns 127.0.0.11:53

defaults
  log global
  mode tcp
  option dontlognull
  # TODO: tune these
  timeout connect 5000
  timeout client 50000
  timeout server 50000
  # allow to boot despite dns don't resolve backends
  default-server init-addr none

frontend stats
  mode http
  bind *:8404
  stats enable
  stats uri /stats
  stats refresh 1s
  stats admin if TRUE

frontend control-plane
  bind *:7777
  
  default_backend kube-apiservers

backend kube-apiservers
  option httpchk GET /healthz
  
  server control-plane-0 1.1.1.1:6443 weight 99 check check-ssl verify none resolvers docker resolve-prefer ipv4
`,
		},
		{
			name: "should return a custom HA config",
			data: &ConfigData{
				FrontendControlPlanePort: "7777",
				BackendControlPlanePort:  "6443",
				BackendServers: map[string]BackendServer{
					"control-plane-0": {
						Address: "1.1.1.1",
						Weight:  99,
					},
				},
			},
			configTemplate: `# generated by kind
global
  log /dev/log local0
  log /dev/log local1 notice
  daemon
  # limit memory usage to approximately 18 MB
  # (see https://github.com/kubernetes-sigs/kind/pull/3115)
  maxconn 100000

resolvers docker
  nameserver dns 127.0.0.11:53

defaults
  log global
  mode tcp
  option dontlognull
  # TODO: tune these
  timeout connect 5000
  timeout client 50000
  timeout server 50000
  # allow to boot despite dns don't resolve backends
  default-server init-addr none

frontend control-plane
  bind *:{{ .FrontendControlPlanePort }}
  {{ if .IPv6 -}}
  bind :::{{ .FrontendControlPlanePort }};
  {{- end }}
  default_backend kube-apiservers

backend kube-apiservers
  option httpchk GET /healthz
  # TODO: we should be verifying (!)
  {{range $server, $backend := .BackendServers}}
  server {{ $server }} {{ JoinHostPort $backend.Address $.BackendControlPlanePort }} check check-ssl verify none resolvers docker resolve-prefer {{ if $.IPv6 -}} ipv6 {{- else -}} ipv4 {{- end }} weight {{ $backend.Weight }}
  {{- end}}

frontend rke2-join
  bind *:9345
  {{ if .IPv6 -}}
  bind :::9345;
  {{- end }}
  default_backend rke2-servers
backend rke2-servers
  option httpchk GET /v1-rke2/readyz
  http-check expect status 403
  {{range $server, $backend := .BackendServers}}
  server {{ $server }} {{ $backend.Address }}:9345 check check-ssl verify none resolvers docker resolve-prefer {{ if $.IPv6 -}} ipv6 {{- else -}} ipv4 {{- end }} weight {{ $backend.Weight }}
  {{- end}}
`,
			expectedConfig: `# generated by kind
global
  log /dev/log local0
  log /dev/log local1 notice
  daemon
  # limit memory usage to approximately 18 MB
  # (see https://github.com/kubernetes-sigs/kind/pull/3115)
  maxconn 100000

resolvers docker
  nameserver dns 127.0.0.11:53

defaults
  log global
  mode tcp
  option dontlognull
  # TODO: tune these
  timeout connect 5000
  timeout client 50000
  timeout server 50000
  # allow to boot despite dns don't resolve backends
  default-server init-addr none

frontend control-plane
  bind *:7777
  
  default_backend kube-apiservers

backend kube-apiservers
  option httpchk GET /healthz
  # TODO: we should be verifying (!)
  
  server control-plane-0 1.1.1.1:6443 check check-ssl verify none resolvers docker resolve-prefer ipv4 weight 99

frontend rke2-join
  bind *:9345
  
  default_backend rke2-servers
backend rke2-servers
  option httpchk GET /v1-rke2/readyz
  http-check expect status 403
  
  server control-plane-0 1.1.1.1:9345 check check-ssl verify none resolvers docker resolve-prefer ipv4 weight 99
`,
		},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(*testing.T) {
			config, err := Config(tc.data, tc.configTemplate)
			g.Expect(err).NotTo(HaveOccurred())
			g.Expect(config).To(Equal(tc.expectedConfig))
		})
	}
}
